package com.hfxt.controller;

import com.alibaba.fastjson.JSON;
import com.hfxt.entity.Bill;
import com.hfxt.entity.Provider;
import com.hfxt.entity.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hfxt.service.BillService;
import com.hfxt.service.ProviderService;
import com.hfxt.util.PageUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {
    private final Logger logger = Logger.getLogger(BillController.class);
    @Resource
    private BillService billService;
    @Resource
    private ProviderService providerService;

    //订单管理  条件分页
    @RequestMapping("/billList")
    public ModelAndView userList(String productName, Integer providerId, Integer isPayment, Integer pageIndex, Integer pageSize) {
        System.out.println("productName------" + productName);
        System.out.println("providerId------" + providerId);
        System.out.println("isPayment------" + isPayment);
        System.out.println("pageIndex------" + pageIndex);
        System.out.println("pageSize------" + pageSize);
        ModelAndView modelAndView = new ModelAndView();
        PageUtil<Bill> pageUtil = new PageUtil<Bill>();
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        List<Bill> billList = billService.queryAllBillsByCondition(productName, providerId, isPayment, pageIndex, pageSize);
        List<Provider> providerList = providerService.queryAllProviders();
        int totalCount = billService.queryCountByCondition(productName, providerId, isPayment);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setPageSize(pageSize);
        pageUtil.setLists(billList);
        pageUtil.setTotalCount(totalCount);
        /*输出pageUtil测试*/
        System.out.println("pageUtil-----" + pageUtil);
        modelAndView.addObject("providerList", providerList);
        modelAndView.addObject("isPayment", isPayment);
        modelAndView.addObject("pageUtil", pageUtil);
        modelAndView.setViewName("billList");
        return modelAndView;
    }

    //查询订单个人详细信息
    @RequestMapping("/view")
    public ModelAndView queryBillById(Long id) {
        System.out.println("queryBillById----id----" + id);
        ModelAndView modelAndView = new ModelAndView();
        Bill bill = billService.queryBillById(id);
        modelAndView.addObject("bill", bill);
        modelAndView.setViewName("billView");
        return modelAndView;
    }

    //删除订单
    @ResponseBody
    @RequestMapping("/deleteBillById")
    public int deleteBillById(Long id) {
        System.out.println("进入订单删除方法！要删除的订单的id=" + id);
        return billService.deleteBillById(id);
    }

    //加载订单修改界面
    @RequestMapping("/toBillUpdate")
    public String toBillUpdate(Long id, Model model) {
        Bill bill = billService.queryBillById(id);
        System.out.println("需要修改的bill---" + bill);
        model.addAttribute("billUpdate", bill);
        List<Provider> providerList = providerService.queryAllProviders();
        model.addAttribute("providerList", providerList);
        model.addAttribute("billList", JSON.toJSONString(billService.queryAllBills()));
        return "billUpdate";
    }

    //修改订单信息
    @ResponseBody
    @RequestMapping("/billUpdate")
    public int billUpdate(Bill bill, HttpSession session) {
        User user = (User) session.getAttribute("user");
        bill.setModifyBy(user.getId());
        bill.setModifyDate(new Date());
        System.out.println("修改页面传入的bill---" + bill);
        return billService.updateBill(bill);
    }

    //加载订单新增界面
    @RequestMapping("/toBillAdd")
    public String toBillAdd(Model model) {
        List<Provider> providerList = providerService.queryAllProviders();
        model.addAttribute("providerList", providerList);
        model.addAttribute("billList", JSON.toJSONString(billService.queryAllBills()));
        return "billAdd";
    }

    //新增订单
    @ResponseBody
    @RequestMapping("/billAdd")
    public int userAdd(Bill bill, HttpSession session) {
        User user = (User) session.getAttribute("user");
        bill.setCreatedBy(user.getId());
        bill.setCreationDate(new Date());
        System.out.println("新增页面传入的bill---" + bill);
        return billService.addBill(bill);

    }

    //查询供应商编码是否存在
    @ResponseBody
    @RequestMapping("/queryBillCode")
    public boolean queryBillCode(String billCode) {
        Bill bill = billService.queryBillByProCode(billCode);
        if (bill != null) {
            System.out.println("该订单编码存在！");
            return true;
        } else {
            System.out.println("该订单编码不存在！");
            return false;
        }
    }


}
